<?php
/*------------------------------------------------------------------------------
Copyright (C) 2006 J.Ducastel <jducastel@jducastel.fr>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
------------------------------------------------------------------------------*/
pathnode::heritage('yamlfilenode',20);
/**
* yaml file handler
* @use datapath for data path syntax
* @use spyc for yaml conversion
* @author J.Ducastel <jducastel@jducastel.fr>
* @version 2006-10-13 10:38:36
*/
class yamlFileNode extends fileNode {
/*------------------------------------------------------------------------------
                                                                     constructor
------------------------------------------------------------------------------*/
	/**
	*
	* @access
	* /
	function () {
		$this->__construct();
	}/**/
	
	/**
	* real constructor
	* @access
	* /
	function __construct() {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                  public methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access public
	* @return bool
	*/
	function load() {
		if ($this->sync)
			return true;
		if (!class_exists('spyc')) {
			trigger_error('Missing required class "spyc"',E_USER_WARNING);
			// exit;
			return false;
		}
		$str=$this->read(); //echo $str;
		$data=spyc::yamlLoad($str);
		if (is_array($data)) {
			$this->data=&$data;
			$this->sync=true;
		}
		return true;
	}/**/
	
	/**
	* save data to yaml file
	* @param
	* @access public
	* @return bool
	*/
	function save() {
		if ($this->sync)
			// not needed
			return true;
		// is writable ?
		if (!$this->isWritable())
			return false;
		// rewinding handle
		$this->toStart();
		$this->erase();
		// converting data to yaml syntax
		$yaml=spyc::yamlDump($this->data,2,255);
		// writing to file
		if ($this->write($yaml))
			$this->sync=true;
		return $this->sync;
	}/**/
	
	/**
	* are file and data synchronized ?
	* @param
	* @access public
	* @return bool
	*/
	function isUpToDate() {
		return $this->sync;
	}/**/
	
	/**
	* return data at targeted path. See datapath for syntax
	* @param string $path
	* @access public
	* @return mixed
	*/
	function getData($path='') {
		if (!$this->sync)
			$this->load();
		return datapath::get($this->data,$path);
	}/**/
	
	/**
	* sets some data
	* @param mixed &$value
	* @param string $path
	* @access public
	* @return 
	*/
	function setData($value,$path='') {
		$set=datapath::set($this->data,$value,$path);
		if ($set)
			$this->sync=false;
		return $set;
	}/**/
	
	/**
	* 
	* @param
	* @access public
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                      properties
------------------------------------------------------------------------------*/
	/**
	* @var array content array
	*/
	var $data=array();
	/**
	* @var bool are file and data synchronized
	*/
	var $sync=false;
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* detects if path applies to self class.
	* subclasses HAVE to implement this method
	* @param string $path
	* @access protected
	* @return bool
	*/
	function selfDetector($path) {
		return ereg('\.yml$',$path);
	}/**/

	/**
	* 
	* @param
	* @access protected
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access protected
	* @return 
	* /
	function () {
		
	}/**/
/*------------------------------------------------------------------------------
                                                                 private methods
------------------------------------------------------------------------------*/
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
	
	/**
	* 
	* @param
	* @access private
	* @return 
	* /
	function () {
		
	}/**/
}
?>
